<?php

namespace app\api\extend;

/**
 * 响应类
 * Class Response
 * @OA\Schema(
 *     title="Response",
 *     description="响应信息",
 * )
 */
class Response
{
    /**
     * @var integer
     * @OA\Property(title="code", description="返回码", default=0)
     */
	public $code;

    /**
     * @var string
     * @OA\Property(title="msg", description="返回消息", default="成功")
     */
	public $msg;

    /**
     * @var mixed
     * @OA\Property(title="data", description="返回数据", default="[]")
     */
	public $data;

    /**
     * @var integer
     * @OA\Property(title="count", description="数量", default=0)
     */
	public $count;

	// 默认常量
	const DEFAULT_CODE_SUCCESS = 0;
	const DEFAULT_CODE_ERROR = 500;
	const DEFAULT_MSG_SUCCESS = '成功';
	const DEFAULT_MSG_ERROR = '失败';
	const DEFAULT_DATA = [];
	const DEFAULT_COUNT = 0;

	public static function setError($msg=self::DEFAULT_MSG_ERROR, $code=self::DEFAULT_CODE_ERROR, $data=self::DEFAULT_DATA) {
		return (new self)
			->setCode($code)
			->setMsg($msg)
			->setData($data)
			->getResponse();
	}

	public static function setSuccess($data=self::DEFAULT_DATA, $code=self::DEFAULT_CODE_SUCCESS) {
		return (new self)
			->setCode($code)
			->setMsg(self::DEFAULT_MSG_SUCCESS)
			->setData($data)
			->getResponse();
	}

    public static function setSuccessWithCount($data=self::DEFAULT_DATA, $count=self::DEFAULT_COUNT, $code=self::DEFAULT_CODE_SUCCESS) {
        return (new self)
            ->setCode($code)
            ->setMsg(self::DEFAULT_MSG_SUCCESS)
            ->setData($data)
            ->setCount($count)
            ->getResponseWithCount();
    }

	public function getResponse() {
		return [
			'code' => $this->code,
			'msg'  => $this->msg,
			'data' => $this->data,
		];
	}

    public function getResponseWithCount() {
        return [
            'code' => $this->code,
            'msg'  => $this->msg,
            'data' => [
                'list' => $this->data,
                'count'=> $this->count,
            ],
        ];
    }

	public function setCount($count) {
		$this->count = $count;
		return $this;
	}

	public function setMsg($msg) {
		$this->msg = $msg;
		return $this;
	}

	public function setCode($code) {
		$this->code = $code;
		return $this;
	}

	public function setData($data) {
		$this->data = $data;
		return $this;
	}
}